package com.cnswhy.cloud.server.comsumer.receiver;


import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.logging.constant.MDCConstant;
import com.cnswhy.cloud.common.rabbitmq.constant.MQConstant;
import com.cnswhy.cloud.common.rabbitmq.model.DLXMessage;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.mq.SmsSendMQ;
import com.cnswhy.cloud.server.comsumer.event.SmsSendMessageEvent;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.cnswhy.cloud.common.rabbitmq.constant.MQConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME;


/**
 * 发送短信
 *
 * @author fk
 * @version v2.0
 * @since v7.0.0 2018年3月23日 上午10:32:15
 */
@Component
@Slf4j
public class RepeatTradeQueueReceiver {
    @Autowired
    MessageService messageService;

   /* *//**
     * 私信队列转发队列
     *
     * @param message
     *//*
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = DEFAULT_REPEAT_TRADE_QUEUE_NAME),
            exchange = @Exchange(value = MQConstant.DEFAULT_EXCHANGE, type = ExchangeTypes.DIRECT)
    ))
    public void sendMessage(DLXMessage message) throws Exception {
        try {
            messageService.sendQueueByObject(message.getExchange(), message.getObject());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    @RabbitHandler
    @RabbitListener(queues = MQConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME)
    public void process1(Message message, Channel channel) throws Exception {
        try {
            log.info(new String(message.getBody()));
            DLXMessage dlxMessage = JSON.parseObject(message.getBody(), DLXMessage.class);
            messageService.sendQueueByObject(dlxMessage.getExchange(), JSON.parseObject(dlxMessage.getContent(),dlxMessage.getClazz()));
            //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }
}
